package com.chris.easymallcoupon.service.impl;

import com.chris.common.to.MemberPrice;
import com.chris.common.to.SkuFullReductionTo;
import com.chris.easymallcoupon.entity.MemberPriceEntity;
import com.chris.easymallcoupon.entity.SkuLadderEntity;
import com.chris.easymallcoupon.service.MemberPriceService;
import com.chris.easymallcoupon.service.SkuLadderService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.chris.common.utils.PageUtils;
import com.chris.common.utils.Query;

import com.chris.easymallcoupon.dao.SkuFullReductionDao;
import com.chris.easymallcoupon.entity.SkuFullReductionEntity;
import com.chris.easymallcoupon.service.SkuFullReductionService;


@Service("skuFullReductionService")
public class SkuFullReductionServiceImpl extends ServiceImpl<SkuFullReductionDao, SkuFullReductionEntity> implements SkuFullReductionService {

    @Autowired
    private MemberPriceService memberPriceService;
    @Autowired
    private SkuLadderService skuLadderService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<SkuFullReductionEntity> page = this.page(
                new Query<SkuFullReductionEntity>().getPage(params),
                new QueryWrapper<SkuFullReductionEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public void saveInfo(SkuFullReductionTo skuFullReductionTo) {
        SkuLadderEntity skuLadderEntity = new SkuLadderEntity();
        BeanUtils.copyProperties(skuFullReductionTo, skuLadderEntity);
        skuLadderEntity.setAddOther(skuFullReductionTo.getCountStatus());
        if (skuLadderEntity.getFullCount() != null && skuLadderEntity.getFullCount() > 0) {
            skuLadderService.save(skuLadderEntity);
        }
        SkuFullReductionEntity skuFullReductionEntity = new SkuFullReductionEntity();
        BeanUtils.copyProperties(skuFullReductionTo, skuFullReductionEntity);
        if (skuFullReductionEntity.getFullPrice() != null && BigDecimal.ZERO.compareTo(skuFullReductionEntity.getFullPrice()) < 0) {
            this.save(skuFullReductionEntity);
        }
        List<MemberPrice> memberPrice = skuFullReductionTo.getMemberPrice();
        if (memberPrice != null && memberPrice.size() > 0) {
            memberPriceService.saveBatch(memberPrice.stream().map(item -> {
                MemberPriceEntity memberPriceEntity = new MemberPriceEntity();
                memberPriceEntity.setMemberLevelId(item.getId());
                memberPriceEntity.setMemberLevelName(item.getName());
                memberPriceEntity.setMemberPrice(item.getPrice());
                memberPriceEntity.setSkuId(skuFullReductionTo.getSkuId());
                return memberPriceEntity;
            }).filter(memberPriceEntity -> memberPriceEntity.getMemberPrice() != null && BigDecimal.ZERO.compareTo(memberPriceEntity.getMemberPrice()) < 0)
                    .collect(Collectors.toList()));
        }
    }

}